function inicializarPooling() {
  obterGuiasXDia();
  obterGuiasAgora();
  obterPagamentosIndevidosXDia();
  obterPagamentosIndevidosAgora();
  obterSumarioMonetarioXDia();
  obterSumarioMonetarioAgora();
}

function dataHoraServidor() {
  var params = new Hash();
  var result = null;
  
  if (Prototype.Browser.IE) {
    params.set("cachebuster",new Date().getTime());
  }
  
  new Ajax.Request("dhs.php"
                  ,{ method: "post"
                   , onCreate: function (xhrObject,jsonResponseHeader) { } 
                   , onSuccess: function(xhrObject,jsonResponseHeader) { result = xhrObject.responseText.evalJSON(); }
                   , parameters: params
                   , asynchronous: false
                   }
                  );
  return result; 
}

//Object.extend(Math, {
//  formatNumber:function(n, options){
//    var options = Object.extend({
//      decimals:0,
//      currency:false,
//      currencySymbol: '$ ',
//      formatWhole:true,
//      wholeDelimiter:',',
//      decimalDelimiter:'.'
//    },options);
// 
//    var nArr = new Array();
//    nArr = String(n).split('.');
// 
//    var whole = (typeof nArr[0]!='undefined')?nArr[0]:'0';
//    if(options.formatWhole){
//      var exp = /(\d+)(\d{3})/;
//      while (exp.test(whole)) {
//        whole = whole.replace(exp, '$1' + options.wholeDelimiter + '$2');
//      }
//    }
// 
//    if(typeof nArr[1]!='undefined'){
//      var remainder = nArr[1];
//    }else{
//      var remainder = '';
//      for(var i=0;i<options.decimals;i++){remainder += '0'}
//    }
// 
//    var pfix = options.currency?options.currencySymbol:'';
// 
//    if(options.decimals<=0) return pfix + whole;
// 
//    var a = new Array();
//    for(var i = 0; i < options.decimals; i++){
//      if(remainder.charAt(i) != '') a[i] = remainder.charAt(i);
//      else a[i] = '0';
//    }
// 
//    return pfix + whole + options.decimalDelimiter + a.join("");
//  }
//});
///////////////////////////////////////////////////////////////////////
function obterGuiasXDia() {
  var hoje = dataHoraServidor().date.substring(0,10);
    
  if (hoje != guiasXDiaDiaAtual) {
    var params = new Hash();
    
    if (Prototype.Browser.IE) {
      params.set("cachebuster",new Date().getTime());
    }
    
    new Ajax.Request("guiasxdia.php"
                    ,{ method: "post"
                     , onCreate: function (xhrObject,jsonResponseHeader) { guiasXDia.showLoading("Mudança de dia detectada!<br>Atualizando o gráfico com novos dados<br>Por favor, queira aguardar..."); } 
                     , onSuccess: function(xhrObject,jsonResponseHeader) { guiasXDia.hideLoading(); atualizarGuiasXDia(xhrObject.responseText.evalJSON(),hoje); }
                     , parameters: params
                     }
                    );
  } else {
    delayedObterGuiasXDia();
  }
}

function atualizarGuiasXDia(aJSON,aHoje) {
  guiasXDia.xAxis[0].setCategories(aJSON.rotulos,false); 
  guiasXDia.series[0].setData(aJSON.pagas,false);
  guiasXDia.series[1].setData(aJSON.geradas,false);
  guiasXDia.redraw();
  guiasXDiaDiaAtual = aHoje;

  delayedObterGuiasXDia();  
}

function delayedObterGuiasXDia() {
  setTimeout("obterGuiasXDia()",10 * 1000);
}
///////////////////////////////////////////////////////////////////////
function obterGuiasAgora() {
  var params = new Hash();
  
  if (Prototype.Browser.IE) {
    params.set("cachebuster",new Date().getTime());
  }
  
  new Ajax.Request("guiasagora.php"
                  ,{ method: "post"
                   , onCreate: function (xhrObject,jsonResponseHeader) { guiasAgora.showLoading("<img src=\"img/ajax-loader.gif\">"); } 
                   , onSuccess: function(xhrObject,jsonResponseHeader) { guiasAgora.hideLoading(); atualizarGuiasAgora(xhrObject.responseText.evalJSON()); }
                   , parameters: params
                   }
                  );
}

function atualizarGuiasAgora(aJSON) {
  guiasAgora.setTitle(null, {text: "(" + aJSON.dta + ")"});
  guiasAgora.xAxis[0].setCategories(aJSON.hms,false);
  guiasAgora.series[0].setData(aJSON.pga,false);
  guiasAgora.series[1].setData(aJSON.ger,false);
  guiasAgora.redraw();
  delayedObterGuiasAgora();  
}

function delayedObterGuiasAgora() {
  setTimeout("obterGuiasAgora()",10 * 1000);
}
///////////////////////////////////////////////////////////////////////
function obterPagamentosIndevidosXDia() {
  var hoje = dataHoraServidor().date.substring(0,10);
    
  if (hoje != pagamentosIndevidosXDiaDiaAtual) {
    var params = new Hash();
    
    if (Prototype.Browser.IE) {
      params.set("cachebuster",new Date().getTime());
    }
    
    new Ajax.Request("pagamentosindevidosxdia.php"
                    ,{ method: "post"
                     , onCreate: function (xhrObject,jsonResponseHeader) { pagamentosIndevidosXDia.showLoading("<img src=\"img/ajax-loader.gif\">"); } 
                     , onSuccess: function(xhrObject,jsonResponseHeader) { pagamentosIndevidosXDia.hideLoading(); atualizarPagamentosIndevidosXDia(xhrObject.responseText.evalJSON(),hoje); }
                     , parameters: params
                     }
                    );
  } else {
    delayedPagamentosIndevidosXDia();
  }
}

function atualizarPagamentosIndevidosXDia(aJSON,aHoje) {
  pagamentosIndevidosXDia.xAxis[0].setCategories(aJSON.rotulos,false); 
  pagamentosIndevidosXDia.series[0].setData(aJSON.dados,false);
  pagamentosIndevidosXDia.redraw();
  pagamentosIndevidosXDiaDiaAtual = aHoje;
  
  delayedPagamentosIndevidosXDia();  
}

function delayedPagamentosIndevidosXDia() {
  setTimeout("obterPagamentosIndevidosXDia()",10 * 1000);
}
///////////////////////////////////////////////////////////////////////
function obterPagamentosIndevidosAgora() {
  var params = new Hash();
  
  if (Prototype.Browser.IE) {
    params.set("cachebuster",new Date().getTime());
  }
  
  new Ajax.Request("pagamentosindevidosagora.php"
                  ,{ method: "post"
                   , onCreate: function (xhrObject,jsonResponseHeader) { guiasAgora.showLoading("<img src=\"img/ajax-loader.gif\">"); } 
                   , onSuccess: function(xhrObject,jsonResponseHeader) { guiasAgora.hideLoading(); atualizarPagamentosIndevidosAgora(xhrObject.responseText.evalJSON()); }
                   , parameters: params
                   }
                  );
}

function atualizarPagamentosIndevidosAgora(aJSON) {
  pagamentosIndevidosAgora.setTitle(null, {text: "(" + aJSON.dta + ")"});
  pagamentosIndevidosAgora.xAxis[0].setCategories(aJSON.hms,false);
  pagamentosIndevidosAgora.series[0].setData(aJSON.pga,false);
  pagamentosIndevidosAgora.redraw();
  delayedObterPagamentosIndevidosAgora();  
}

function delayedObterPagamentosIndevidosAgora() {
  setTimeout("obterPagamentosIndevidosAgora()",10 * 1000);
}
///////////////////////////////////////////////////////////////////////
function obterSumarioMonetarioXDia() {
  
  var hoje = dataHoraServidor().date.substring(0,10);
    
  if (hoje != sumarioMonetarioDiaAtual) {
    var params = new Hash();
    
    if (Prototype.Browser.IE) {
      params.set("cachebuster",new Date().getTime());
    }
    
    new Ajax.Request("sumariomonetarioxdia.php"
                    ,{ method: "post"
                     , onCreate: function (xhrObject,jsonResponseHeader) { sumarioMonetarioXDia.showLoading("<img src=\"img/ajax-loader.gif\">"); } 
                     , onSuccess: function(xhrObject,jsonResponseHeader) { sumarioMonetarioXDia.hideLoading(); atualizarSumarioMonetarioXDia(xhrObject.responseText.evalJSON(),hoje); }
                     , parameters: params
                     }
                    );
  } else {
    delayedObterGuiasXDia();
  }
}

function atualizarSumarioMonetarioXDia(aJSON,aHoje) {
//  var emolumento = Math.formatNumber(aJSON.emolumento,{decimals:2
//                                                      ,currency: false
//                                                      ,currencySymbol:'R$ '
//                                                      ,formatWhole: true
//                                                      ,wholeDelimiter:'.'
//                                                      ,decimalDelimiter:','});

  sumarioMonetarioXDia.xAxis[0].setCategories(aJSON.rotulos,false); 
  sumarioMonetarioXDia.series[0].setData(aJSON.emol,false);
  sumarioMonetarioXDia.series[1].setData(aJSON.tsnr,false);
  sumarioMonetarioXDia.series[2].setData(aJSON.ferc,false);
  sumarioMonetarioXDia.redraw();

  sumarioMonetarioDiaAtual = aHoje;

  delayedObterSumarioMonetarioXDia();  
}

function delayedObterSumarioMonetarioXDia() {
  setTimeout("obterSumarioMonetarioXDia()",10 * 1000);
}
///////////////////////////////////////////////////////////////////////
function obterSumarioMonetarioAgora() {
  var params = new Hash();
  
  if (Prototype.Browser.IE) {
    params.set("cachebuster",new Date().getTime());
  }
  
  new Ajax.Request("sumariomonetarioagora.php"
                  ,{ method: "post"
                   , onCreate: function (xhrObject,jsonResponseHeader) { sumarioMonetarioAgora.showLoading("<img src=\"img/ajax-loader.gif\">"); } 
                   , onSuccess: function(xhrObject,jsonResponseHeader) { sumarioMonetarioAgora.hideLoading(); atualizarSumarioMonetarioAgora(xhrObject.responseText.evalJSON()); }
                   , parameters: params
                   }
                  );
}

function atualizarSumarioMonetarioAgora(aJSON) {
  sumarioMonetarioAgora.setTitle(null, {text: "(" + aJSON.dta + ")"});
  sumarioMonetarioAgora.xAxis[0].setCategories(aJSON.hms,false);
  
  sumarioMonetarioAgora.series[0].setData(aJSON.emol,false);
  sumarioMonetarioAgora.series[1].setData(aJSON.tsnr,false);
  sumarioMonetarioAgora.series[2].setData(aJSON.ferc,false);
  
  sumarioMonetarioAgora.redraw();
  delayedObterSumarioMonetarioAgora();  
}

function delayedObterSumarioMonetarioAgora() {
  setTimeout("obterSumarioMonetarioAgora()",10 * 1000);
}
